﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using Tenancy.Helper;
using System.Data;
using SQLServer.DBUtility;

namespace Tenancy.Server
{
    /// <summary>
    /// Hbung_CanCelOrder 的摘要说明
    /// </summary>
    public class Hbung_CanCelOrder : IHttpHandler
    {
        /// <summary>
        /// 客户取消订单接口
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //用户登录的手机号
            string Hbung_Tel = context.Request.Params["Tel"];
            string Hbung_Phone = string.Empty;
            if (string.IsNullOrEmpty(Hbung_Tel))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Tel为空"));
            }
            else
            {
                string pattern = "^1[0-9]{10}$"; //正则表达式字符串
                Regex cgsregex = new Regex(pattern);
                Hbung_Phone = DESEncrypt.MD5Decrypt(Hbung_Tel);
                bool isMatch = cgsregex.IsMatch(Hbung_Phone);
                if (!isMatch)
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Tel格式不正确"));
                    return;
                }
            }
            //签名
            string HBsign = context.Request.Params["Sign"];
            if (string.IsNullOrEmpty(HBsign))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Sign为空"));
                return;
            }
            else
            {
                if (GenerClass.IsOkMatch(Hbung_Phone) != Regex.Replace(HBsign, @"\s", "+"))
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Sign不匹配"));
                    return;
                }
            }

            //取消原因
            string HB_Reason = context.Request.Params["Reason"];
            if (string.IsNullOrEmpty(HB_Reason))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Reason为空"));
                return;
            }

            //订单号
            string HB_OrderNum = context.Request.Params["OrderNum"];
            if(string.IsNullOrEmpty(HB_OrderNum))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("OrderNum为空"));
                return;
            }
            string StrOrder = string.Format("select * from OnlineOrders where OrderNum='{0}' and CPhone='{1}' ", HB_OrderNum, Hbung_Phone);
            DataTable OneOrderData = DbLocalSQL.Query(StrOrder).Tables[0];
            if(OneOrderData!=null&&OneOrderData.Rows.Count>0)
            {
                string UpStr = string.Format("update OnlineOrders set OrderStatus='2',CancelCategory='客户取消',CancelReason='{0}' where OrderNum='{1}' and CPhone='{2}' and (OrderStatus='0' or OrderStatus='3' ) ", HB_Reason, HB_OrderNum, Hbung_Phone);
                if(DbLocalSQL.ExecuteSql(UpStr)>0)
                {
                    //老用户给业务员，行政，区域经理推送
                    if (OneOrderData.Rows[0]["PeoType"].ToString() == "业务员")
                    {
                        if (!string.IsNullOrEmpty(OneOrderData.Rows[0]["SalemanTel"].ToString()))
                        {
                            //给业务员推送
                            Helper.MsgSend.HB_SendMsg(OneOrderData.Rows[0]["SalemanTel"].ToString(), GenerClass.CancelOrder(HB_OrderNum));

                            string OldStr = string.Format("select * from SalesAdminManger where SalesTel='{0}'", OneOrderData.Rows[0]["SalemanTel"].ToString());
                            DataTable OldData = DbLocalSQL.Query(OldStr).Tables[0];
                            if (OldData != null && OldData.Rows.Count > 0)
                            {
                                //给行政推送
                                if (!string.IsNullOrEmpty(OldData.Rows[0]["AdminTel"].ToString()))
                                {
                                    Helper.MsgSend.HB_SendMsg(OldData.Rows[0]["AdminTel"].ToString(), GenerClass.CancelOrder(HB_OrderNum));
                                }
                               
                                //给区域经理推送
                                if (!string.IsNullOrEmpty(OldData.Rows[0]["ManagerTel"].ToString()))
                                {
                                    Helper.MsgSend.HB_SendMsg(OldData.Rows[0]["ManagerTel"].ToString(), GenerClass.CancelOrder(HB_OrderNum));
                                }
                               
                            }

                        }
                    }

                    //新用户区域经理推送

                    if (OneOrderData.Rows[0]["PeoType"].ToString() == "区域经理")
                    {
                        if (!string.IsNullOrEmpty(OneOrderData.Rows[0]["SalemanTel"].ToString()))
                        {
                            Helper.MsgSend.HB_SendMsg(OneOrderData.Rows[0]["SalemanTel"].ToString(), GenerClass.CancelOrder(HB_OrderNum));
                        }
                       
                    }

                    context.Response.Write(GenerClass.SetSucessJsData("取消成功","[]"));
                    return;
                }
                else
                {
                    context.Response.Write(GenerClass.SetChangeErrorJsData("取消失败"));
                    return;
                }
            }
            else
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("订单不存在"));
                return;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}